package com.edeweiss.community.servlet;

import com.edeweiss.community.domain.Notice;
import com.edeweiss.community.services.NoticeService;
import com.edeweiss.community.services.servicesImp.NoticeServiceImp;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/NoticeServlet")
public class NoticeServlet extends BaseServlet {
    private NoticeService noticeService = new NoticeServiceImp();

    public String findNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service层查询公告
        List<Notice> queryNotice =  noticeService.findNotice();

        //将内容存储到session中
        request.getSession().setAttribute("notice", queryNotice);
        response.sendRedirect("pages/withnessNotice.jsp");
        //转发
        return "/pages/withnessNotice.jsp";
    }

    public String deleteNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取要删除的公告id
        String nid = request.getParameter("nid");
        //调用service层的deleteNotice方法删除公告
        noticeService.delNotice(nid);
        //请求转发
        response.sendRedirect("/pages/withnessNotice.jsp");
        return "/withness.jsp";
    }

    //管理员发布公告
    public String insertNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Notice notice =  new Notice();

        notice.setName(request.getParameter("name"));
        notice.setContent(request.getParameter("content"));

        //调用service层的添加方法
        int flag = noticeService.addNotic(notice);
        String msg = flag == 1?"添加成功":"添加失败";
        //请求转发到消息页面
        request.getSession().setAttribute("msg",msg);
        response.sendRedirect("pages/index.jsp");
        return "";
    }
}
